package com.fs.jzzn.pojo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;

import com.fs.base.pojo.PEntity;
import com.fs.base.utils.ExcelUtils;

/**
 * @author TiddlerCJ
 * @desc 发票实体管理
 */
@Entity
@Table(name="p_invoice")
@Data@EqualsAndHashCode(callSuper=false)
public class Invoice extends PEntity {
	private String invoiceNo;	//单据编号
	private String pNo;			//合同编号/工程编号
	private String signNo;		//外径证号
	
	private String billNo;		//总包开票编号
	private String billNoF;	//分包开票编号
	
	private String billTitle;		//总包开票抬头
	private String billTitleF;		//分包开票抬头
	
	private String billDate;		//开票日期
	private String billDateF;	//开票日期
	
	private String billName;	//开票单位
	private String billNameF;	//开票单位
	
	private Double money;		//开票金额
	private Double moneyF;	//开票金额
	
	private String operator;	//经办人
	private String operatorF;	//经办人
	
	private String billType;		//增值税发票类型
	private String billTypeF;	//增值税发票类型
	private String taxRate;		//税率
	private String taxRateF;	//税率
	
	private String department;		//所属部门
	private String url;			//云存储地址
	
	private String billKind;//发票种类  :工程发票或合同发票
	
	@Transient
	private String moneyNoRate;	//不含税金额M =开票金额/(1+税率)
	@Transient
	private String moneyRate;		//税额P= M*税率
	
	public String getMoneyNoRate() {
		try {
			if(getMoney()!=null && StringUtils.isNotBlank(taxRate) && taxRate.endsWith("%")){
				DecimalFormat df  = new DecimalFormat("0.00");
				Double rate = Double.parseDouble(taxRate.replace("%", "")) /100.00;
				moneyNoRate =  df.format(getMoney() /(1+ rate));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return moneyNoRate;
	}
	public String getMoneyRate() {
		try {
			String p = getMoneyNoRate();
			if(p!=null){
				DecimalFormat df  = new DecimalFormat("0.00");
				Double rate = Double.parseDouble(taxRate.replace("%", "")) /100.00;
				moneyRate = df.format(Double.parseDouble(getMoneyNoRate())* rate);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return moneyRate;
	}
	
	
	public static List<Invoice> excelDataFormat(List<List<String>> data) {
		List<Invoice> list = new ArrayList<Invoice>();
		for(List<String> rowData: data){
			//System.out.println(rowData.size());
			list.add(excelData2Bean(rowData));
		}
		//删除标题行
		//if(list.size()>0) { list.remove(0); }
		return list;
	}
	
	//"[\"开票编号(总包)\",\"发票标题(总包)\",\"开票编号(分包)\",\"发票标题(分包)\",\"合同编号\",\"开票人\",\"开票日期\",\"外经证\",\"开票金额\",\"经办人\",\"发票类型\",\"税率\",\"备注\"]";
	private static Invoice excelData2Bean(List<String> row) {
		/*
		不含税金额	
		税额	
		备注	
		所属机构	
		创建时间	
		创建人*/
		
		row = ExcelUtils.dataLenFomart(row, 27);
		
		Invoice inv = new Invoice();
		inv.setInvoiceNo(row.get(0));//单据编号
		inv.setPNo(row.get(1));//对象编号	
		inv.setSignNo(row.get(2));//外经证
		
		/*
		 * 总
		 */
		inv.setBillTitle(row.get(3));//发票标题(总)
		inv.setBillNo(row.get(4));//开票编号(总)
		inv.setBillNo(inv.getBillNo().replace("\n", ","));
		
		
			
		inv.setBillDate(row.get(5));//开票日期(总)
		try {
			Double money = Double.parseDouble(row.get(6));
			inv.setMoney(money);//开票金额(总)
		} catch (Exception e) {
			System.err.println("总开票金额格式有误："+row.get(6) );
		}
		
		inv.setBillType(row.get(7));//发票类型(总)	
		
		
		inv.setTaxRate(row.get(8));//税率(总)
		inv.setBillName(row.get(9));//开票单位(总)
		inv.setOperator(row.get(10));//经办人(总)
		
		
		/*
		 * 分
		 */
		inv.setBillTitleF(row.get(11));//发票标题(分)	
		inv.setBillNoF(row.get(12));//开票编号(分)
		
		
		inv.setBillDateF(row.get(13));//开票日期(分)
		try {
			Double money = Double.parseDouble(row.get(14));
			inv.setMoneyF(money);//开票金额(分)	
		} catch (Exception e) {
			System.err.println("分开票金额格式有误："+row.get(14) );
		}
		inv.setBillTypeF(row.get(15));//发票类型(分)	
		inv.setTaxRateF(row.get(16));//税率(分)	
		inv.setBillNameF(row.get(17));//开票单位(分)	
		inv.setOperatorF(row.get(18));//经办人(分)
		
		//不含税金额19
		//税额20
		//备注21
		inv.setRemark(row.get(21));
		inv.setOrgNo(row.get(22));//机构22
		inv.setCreateTime(new Date());//23
		inv.setCreaterId(row.get(24));;
	
		return inv;
	}
}
